package org.newbee.des.zkclient;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

/**
 * Created by yujie on 2016/10/20.
 */
public class Sub {
    public static void main(String[] args) {
        ZkClient client = new ZkClient("localhost:2181", 6000);
        // client.createPersistent("/zk", "zkclient");

        client.subscribeDataChanges("/zk", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println(s);
                User user = (User) o;
                System.out.println(user);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {

            }
        });
        int count = 0;
        while (true) {
            // client.watchForData("/zk");
            System.out.println("检测");
            client.writeData("/zk", User.of("zkclient", count + ""));
            count++;
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
